package com.practice.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.practice.reggie.common.BaseContext;
import com.practice.reggie.common.R;
import com.practice.reggie.entity.ShoppingCart;
import com.practice.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 列表查询
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 购物车添加
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //2.查询当前菜品或者套餐是否在购物车中
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);

        if (serviceOne!=null){
            //3.如果已经存在，就在原来数量基础上加一：update xxx set number = number + 1
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number+1);
            shoppingCartService.updateById(serviceOne);
        }else{
            //4.如果不存在，则添加到购物车，数量默认就是一: insert
            //添加购物车
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            serviceOne = shoppingCart;
        }
        return R.success(serviceOne);
    }

    @DeleteMapping("clean")
    public R<String> del(){
        //获取当前登录用户
        Long currentId = BaseContext.getCurrentId();
        //获取条件构造器
        LambdaQueryWrapper<ShoppingCart> updateWrapper = new LambdaQueryWrapper<ShoppingCart>();
        updateWrapper.eq(ShoppingCart::getUserId,currentId);
        //根据id删除购物车数据
        shoppingCartService.remove(updateWrapper);
        //返回
        return R.success("清空购物车成功！");
    }

    /**
     * 购物车减号 -------------
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //{dishId: "1397851668262465537", setmealId: null}
        //{dishId: null, setmealId: "1452915156793466882"}
        log.info("购物车数据:{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id =?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null) {
            Integer number = cartServiceOne.getNumber();
            //number大于1 则数量减一
            if (number > 1) {
                cartServiceOne.setNumber(number - 1);
                shoppingCartService.updateById(cartServiceOne);
            }
            //number为1 则删除该项
            if (number == 1) {
                shoppingCartService.removeById(cartServiceOne);
            }
        } else {
            R.error("购物车没有该项");
        }
        return R.success(cartServiceOne);
    }
}